/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.lazy;

import com.rbh.risk.entity.RiskData;
import com.rbh.risk.service.RiskDataService;
import java.util.List;
import java.util.Map;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author M
 */
@Service("lazyRiskDataModel")
public class LazyRiskDataModel extends LazyDataModel<RiskData> {

    @Autowired
    private RiskDataService riskDataService;
    private String[] dateFilter;

    public LazyRiskDataModel() {
        super();
    }

    @Override
    public RiskData getRowData(String rowKey) {
        return riskDataService.getById(rowKey);
    }

    @Override
    public Object getRowKey(RiskData risk) {
        return risk.getDataId();
    }

    @Override
    public List<RiskData> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {

        if (dateFilter != null && dateFilter.length > 0) {
            filters.put("dateFilter", (dateFilter[0] + "," + dateFilter[1]));
        } else {
            return null;
        }

        List<RiskData> results = riskDataService.getByFilter(first, pageSize, sortField, sortOrder.equals(SortOrder.ASCENDING) ? true : false, filters);
        super.setRowCount(riskDataService.getMaxResult(filters));
        return results;
    }

    public String[] getDateFilter() {
        return dateFilter;
    }

    public void setDateFilter(String[] dateFilter) {
        this.dateFilter = dateFilter;
    }
}